package br.com.db;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import org.apache.http.client.ClientProtocolException;

import com.dropbox.core.DbxClient;
import com.dropbox.core.DbxDelta;
import com.dropbox.core.DbxDelta.Entry;
import com.dropbox.core.DbxEntry;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;

public class DBTester {
	
	private static final String ACCESS_TOKEN = "cXJce5YrLLkAAAAAAAAAARou5oTnTRdvR-FuRtkKGU1jgyUT61v122y2KmQrxLW9";

	/**
	 * @param args
	 * @throws DbxException 
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static void main(String[] args) throws DbxException, ClientProtocolException, IOException {
		
        DbxRequestConfig config = new DbxRequestConfig("JavaTutorial/1.0", Locale.getDefault().toString());
        
		DbxClient client = new DbxClient(config, ACCESS_TOKEN);
		System.out.println("Linked account: " + client.getAccountInfo().displayName);
		
		DbxDelta<DbxEntry> dbxDelta = client.getDelta(null);
		List<Entry<DbxEntry>> entries = dbxDelta.entries;
		String cursor = dbxDelta.cursor;
		System.out.println(cursor);
		
		while(dbxDelta.hasMore){
			dbxDelta = client.getDelta(cursor);
			entries.addAll(dbxDelta.entries);
			cursor = dbxDelta.cursor;
			System.out.println(cursor);
		}
		
		for (Entry<DbxEntry> entry : entries) {
			if(entry.metadata == null){
				System.out.println(entry.lcPath + ": path removed.");
			}
			else{
				if(entry.metadata.isFile()){
					if(getEntryFolder(entry).equals("/photos/")){
						System.out.println(entry.metadata.asFile().name);
					}
				}
			}
		}
	}

	private static String getEntryFolder(Entry<DbxEntry> entry) {
		String[] stringArray = entry.lcPath.split(entry.metadata.asFile().name.toLowerCase());
		return stringArray[0];
	}
}
